﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using AKJ.Sharepoint.Branding.BrandingInterceptor;
using AKJ.Sharepoint.Branding;
using AKJ.Sharepoint.Branding.Config;
using System.Collections;
using System.Security.Policy;
using Microsoft.SharePoint.Utilities;
using System.IO;
using System.Xml;
using System.Configuration;
using AKJ.Sharepoint.Branding.Code;

namespace AKJ.Sharepoint.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            //StreamReader reader = File.OpenText(@"c:\xml.txt");
            //string xml = reader.ReadToEnd();

            bool isLayoutPath = "".Contains("_layouts/");

            XmlDocument config = new XmlDocument();
            config.Load(@"c:\xml.config");

            string currentUrl = "/default.aspx";

            XmlNodeList elements = config.SelectNodes("//VisualBranding/RebrandPaths/RebrandPath");

            int counter = 0;
            foreach (XmlElement element in elements)
            {
                bool includelayoutsFolder = element.GetAttribute("IncludelayoutsFolder") == "" ? false : bool.Parse(element.GetAttribute("IncludelayoutsFolder"));
                string path = element.GetAttribute("Path");
                int priority = element.GetAttribute("Priority") == "" ? 0 : int.Parse(element.GetAttribute("Priority"));

                // If this is a layouts page abort if it is not permitted
                if (isLayoutPath && (includelayoutsFolder == false))
                        continue;

                // We URLDecode if user has enter a URL like "/Shared%20Documents/Forms/AllItems.aspx" in Web.config
                if (currentUrl.ToLower().Contains(SPEncode.UrlDecodeAsUrl(path.ToLower())))
                {
                    priority = priority <= 0 ? counter : priority;

                    XmlNode applyMasterNode = element.SelectSingleNode("ApplyMaster");

                    if (applyMasterNode != null)
                    {
                        string masterFile = applyMasterNode.Attributes["File"] == null ? "" : applyMasterNode.Attributes["File"].Value;
                        string masterApplyOnlyTo = applyMasterNode.Attributes["ApplyOnlyTo"] == null ? "" : applyMasterNode.Attributes["ApplyOnlyTo"].Value;
                        
                        if(masterFile != "")
                            BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Master, priority, masterFile);
                    }

                    //string applyCssFile = 

                    
                    // If priority eq -1 then it is undefined. Use running index as priority instead.
                    
                    /*
                    if (rebrandPath.ApplyCss.File != "")
                        BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Css, priority, rebrandPath.ApplyCss.File);

                    if (rebrandPath.ApplyMaster.File != "")
                        BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Master, priority, rebrandPath.ApplyMaster.File);
                     * */
                }

                counter++;
            }

            foreach (XmlElement element in elements)
            {
                /*
                RebrandPathElement rbe = new RebrandPathElement();
                rbe.IncludelayoutsFolder = (element.GetAttribute("IncludelayoutsFolder") == false || element.GetAttribute("IncludelayoutsFolder") == null);

                System.Console.WriteLine("===========");
                string path = element.GetAttribute("Path");
                //int quantitySold = Int32.Parse(element.GetAttribute("quantity"));

                System.Console.WriteLine(path);
                */
                /*string xPathExpression = "//items/item[@productCode='" + productCode + "']";
                XmlElement inventoryItem = (XmlElement)inventory.SelectSingleNode(xPathExpression);

                int quantity = Int32.Parse(inventoryItem.GetAttribute("quantity"));
                quantity -= quantitySold;
                inventoryItem.SetAttribute("quantity", quantity.ToString());*/
            }
        }
    }
}
